﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CYTS.Aoyou.LuceneSearch.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            //CreateIndex();
            Search();
        }

        private static void CreateIndex()
        {
            InitialEvent();
            System.Console.CursorVisible = false;
            System.Console.WriteLine("Begin...");
            IndexManager.CreateIndexAndOptimize();
            System.Console.WriteLine("Done!");
            System.Console.Read();
        }

        private static void Search()
        {
            string searchKeywords = "长滩岛";
            System.Console.WriteLine("Search keywords: " + searchKeywords);
            var result = IndexManager.Search(searchKeywords, 1, 20);
            System.Console.WriteLine("Total hits: " + result.TotalResult);
            if (result.TotalResult > 0)
            {
                foreach (var doc in result.DocList)
                {
                    System.Console.WriteLine("----------------------------------------------------");
                    System.Console.WriteLine("Title : " + doc.Title);
                    System.Console.WriteLine("Highlight : " + doc.Content);
                    System.Console.WriteLine("Date : " + doc.Date.ToString("yyyy-MM-dd"));
                    System.Console.WriteLine();
                }
            }
            System.Console.Read();
        }

        private static void InitialEvent()
        {
            IndexManager.OneDocCreated -= IndexManager_Notify;
            IndexManager.BeginCreate -= IndexManager_BeginCreate;
            IndexManager.BeginDelete -= IndexManager_BeginDelete;
            IndexManager.BeginOptimize -= IndexManager_BeginOptimize;
            IndexManager.CreateComplete -= IndexManager_CreateComplete;
            IndexManager.OptimizeComplete -= IndexManager_OptimizeComplete;

            IndexManager.OneDocCreated += new Action<int>(IndexManager_Notify);
            IndexManager.BeginCreate += new Action(IndexManager_BeginCreate);
            IndexManager.BeginDelete += new Action(IndexManager_BeginDelete);
            IndexManager.BeginOptimize += new Action(IndexManager_BeginOptimize);
            IndexManager.CreateComplete += new Action<int, long>(IndexManager_CreateComplete);
            IndexManager.OptimizeComplete += new Action<long>(IndexManager_OptimizeComplete);
        }

        static void IndexManager_OptimizeComplete(long obj)
        {
            System.Console.WriteLine("Optimization is complete! Elapsed time: " + obj / 1000m + "s");
            System.Console.WriteLine();
        }

        static void IndexManager_CreateComplete(int arg1, long arg2)
        {
            Print("Creation is complete! Total documents: " + arg1 + ". Elapsed time: " + arg2 / 1000m + "s");
            System.Console.WriteLine();
        }

        static void IndexManager_BeginOptimize()
        {
            System.Console.WriteLine("Optimizing...");
            System.Console.WriteLine();
        }

        static void IndexManager_BeginDelete()
        {
            System.Console.WriteLine("Deleting...");
            System.Console.WriteLine();
        }

        static void IndexManager_BeginCreate()
        {
            System.Console.WriteLine("Creating...");
            System.Console.WriteLine();
        }

        static void IndexManager_Notify(int obj)
        {
            if (obj % 200 == 0)
            {
                Print("Process : " + obj);
            }
        }

        static void Print(string text)
        {
            System.Console.SetCursorPosition(0, System.Console.CursorTop - 1);
            System.Console.WriteLine(text);
        }
    }
}
